<?php

namespace app\common\service;

use app\admin\FreightTeplate;
use app\admin\FreightTeplateArea as fta;
use think\Service;

class FreightTeplateService
{

    public function getProFreight($cart = [], $user_address = [])
    {
        $freight_type = $cart['freight_type'] ?? 0;
        switch ($freight_type) {
            case '0':
                return ['status' => 1, 'freight' => floatval($cart['freight_price'])]; //固定运费
                break;
            case '1':
                $num           = $cart['pro_num'] ?? 1;
                $template_info = FreightTeplate::where(array('id' => $cart['freight_tpl']))->find();
                if (!$template_info) {
                    return ['msg' => '运费模板不存在', 'freight' => 0, 'status' => 0];
                }

                $template_area = fta::where(['guan_id' => $cart['freight_tpl']])->select();
                if (!$template_area || empty($template_area)) {
                    return ['msg' => '运费模板未设置配送区域', 'freight' => 0, 'status' => 0];
                }
                if (empty($user_address)) {
                    return ['msg' => '未检测到用户收货地址', 'freight' => 0, 'status' => 0];
                }

                $sheng = $user_address['province'];
                $shi   = $user_address['city'];
                $xian  = $user_address['xian'];

                $tmp_area_id = 0;
                foreach ($template_area as $key => $value) {
                    if ($value['all_area_name'] == "" || $value['all_area_name'] == '全部地区') {
                        //如果有配送到全国地区的
                        $tmp_area_id = $value['id'];
                        break;
                    }
                    if (strpos($value['all_area_name'], $sheng . "(all)") !== false) {
                        $tmp_area_id = $value['id'];
                        break;
                    } else {
                        if (strpos($value['all_area_name'], $shi . "(all)") !== false) {
                            $tmp_area_id = $value['id'];
                            break;
                        } else {
                            if (strpos($value['all_area_name'], "#" . $xian . "#") !== false) {
                                $tmp_area_id = $value['id'];
                                break;
                            }
                        }
                    }
                }

                if ($tmp_area_id > 0) {

                    $template_info = fta::where("id", $tmp_area_id)->find();
                    if ($num > $template_info['first_num']) {
                        $freight1 = floatval($template_info['first_price']);

                        $left_num = $num - $template_info['first_num'];
                        $freight2 = floatval(ceil($left_num / $template_info['second_num']) * $template_info['second_price']);

                        $freight = $freight1 + $freight2;
                    } else {
                        $freight = floatval($template_info['first_price']);
                    }

                    $re['freight'] = $freight;
                    $re['status']  = 1;
                } else {
                    $re['status'] = 0;
                }

                return $re;

                break;
            default:
                return ['status' => 1, 'freight' => 0]; //包邮
                break;
        }
    }
}
